#include "StdAfx.h"
#include "AutoComboBox.h"
#include "AutoCombox.h"

CAutoComboBox::CAutoComboBox(void)
{
}

CAutoComboBox::~CAutoComboBox(void)
{
}

//BOOL CAutoComboBox::PreTranslateMessage(MSG* pMsg)
//{
//	if (pMsg->message == WM_KEYDOWN)
//	{
//		m_bAutoComplete = TRUE;
//
//		int nVirtKey = (int) pMsg->wParam;
//		if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
//			m_bAutoComplete = FALSE;
//	}
//	return TRUE;
//	//return CWindowImpl< CAutoComboBox, CComboBox >::PreTranslateMessage(pMsg);
//}
LRESULT CAutoComboBox::OnEditChange( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	SetMsgHandled( FALSE );
	if (!m_bAutoComplete) 
      return 0L;
 /* BOOL status = GetWindowLongPtr(this->m_hWnd, GWL_STYLE) & CBS_DROPDOWN;
  if (status)
	this->ShowDropDown(TRUE);*/
  // Get the text in the edit box
  CString str;
  GetWindowText(str);
  int nLength = str.GetLength();
  
  // Currently selected range
  DWORD dwCurSel = GetEditSel();
  WORD dStart = LOWORD(dwCurSel);
  WORD dEnd   = HIWORD(dwCurSel);

  // Search for, and select in, and string in the combo box that is prefixed
  // by the text in the edit box
  if (SelectString(-1, str) == CB_ERR)
  {
      SetWindowText(str);		// No text selected, so restore what was there before
      if (dwCurSel != CB_ERR)
        SetEditSel(dStart, dEnd);	//restore cursor postion
  }

  // Set the text selection as the additional text that we have added
  if (dEnd < nLength && dwCurSel != CB_ERR)
      SetEditSel(dStart, dEnd);
  else
      SetEditSel(nLength, -1);
  return 0L;
}
//void CAutoComboBox::SetAutoComplate(BOOL bNew)
//{
//	m_bAutoComplete = bNew;
//	MakeAutocompleteCombo(this->m_hWnd);
//}

